package com.aierjun.ani.init;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Ani_aierJun on 2017/10/11.
 */

public class SystemCacheSizeUtils {
    private ActivityManager manager;
    private ActivityManager.MemoryInfo memoryInfo;

    /*
    * 获得该应用分配的cache大小
    *
    * */
    public int getInitChachSize(Context context) {
        if (manager == null)
            manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        int size = manager.getMemoryClass();
        Log.d("Jun", size + "   size  ");
        return size;
    }

    /*
    * 获得整个系统剩余cache大小
    *
    * */
    public long getRemainCacheSize(Context context) {
        if (manager == null)
            manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        if (memoryInfo == null)
            memoryInfo = new ActivityManager.MemoryInfo();
        manager.getMemoryInfo(memoryInfo);
        Log.d("Jun", memoryInfo.availMem + "   getRemainCacheSize  ");
        return memoryInfo.availMem;
    }

    /*
    * 是否内存不足（不足时，清理ImageLoader的cache）
    *
    * */
    public boolean oom(Context context) {
        if (manager == null)
            manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        if (memoryInfo == null)
            memoryInfo = new ActivityManager.MemoryInfo();
        manager.getMemoryInfo(memoryInfo);
        if (memoryInfo.lowMemory)
            ImageLoader.getInstance().clearMemoryCache();
        Log.d("Jun", memoryInfo.lowMemory + "   lowMemory  ");
        return memoryInfo.lowMemory;
    }
}
